package com.flyco.modules.oms.service.impl;

import com.flyco.modules.oms.entity.OmsOrderBlack;
import com.flyco.modules.oms.entity.OmsOrderBlackArea;
import com.flyco.modules.oms.mapper.OmsOrderBlackAreaMapper;
import com.flyco.modules.oms.mapper.OmsOrderBlackMapper;
import com.flyco.modules.oms.service.IOmsOrderBlackService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 禁止下单表
 * @Author: flyco
 * @Date:   2021-01-14
 * @Version: V1.0
 */
@Service
public class OmsOrderBlackServiceImpl extends ServiceImpl<OmsOrderBlackMapper, OmsOrderBlack> implements IOmsOrderBlackService {

	@Autowired
	private OmsOrderBlackMapper omsOrderBlackMapper;
	@Autowired
	private OmsOrderBlackAreaMapper omsOrderBlackAreaMapper;
	
	@Override
	@Transactional
	public void saveMain(OmsOrderBlack omsOrderBlack, List<OmsOrderBlackArea> omsOrderBlackAreaList) {
		omsOrderBlackMapper.insert(omsOrderBlack);
		for(OmsOrderBlackArea entity:omsOrderBlackAreaList) {
			//外键设置
			entity.setBlackId(omsOrderBlack.getId());
			omsOrderBlackAreaMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(OmsOrderBlack omsOrderBlack,List<OmsOrderBlackArea> omsOrderBlackAreaList) {
		omsOrderBlackMapper.updateById(omsOrderBlack);
		
		//1.先删除子表数据
		omsOrderBlackAreaMapper.deleteByMainId(omsOrderBlack.getId());
		
		//2.子表数据重新插入
		for(OmsOrderBlackArea entity:omsOrderBlackAreaList) {
			//外键设置
			entity.setBlackId(omsOrderBlack.getId());
			omsOrderBlackAreaMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		omsOrderBlackAreaMapper.deleteByMainId(id);
		omsOrderBlackMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			omsOrderBlackAreaMapper.deleteByMainId(Long.valueOf(id.toString()));
			omsOrderBlackMapper.deleteById(id);
		}
	}
	
}
